<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2020-02-21
 * Time: 12:15
 */

namespace app\admin\controller;


use app\common\tools\LogThink;
use library\Controller;
use think\facade\Request;

/**
 * 调试日志查看
 * Class Log
 * @package app\admin\controller
 */
class Log extends Controller
{
    /**
     * 日志查看
     * @auth true
     * @menu true
     * @return string
     */
    public function index(){
        $offset = Request::get('offset',0);
        $file = Request::get('file',null);
        $limit = Request::get('limit',10);
        $log_time = Request::get('log_time');
        $content = Request::get('content');
        $log = new LogThink($file);
        if(!empty($log_time) || !empty($content)){
            $list = $log->fetch(0,100000,4096,$content,$log_time);
        }else{
            $list = $log->fetch($offset,$limit);
        }
        $files = $log->getLogFiles();
        if(is_null($file)){
            $file = $log->getLastModifiedLog();
        }
        return $this->fetch('',[
            'list'=>$list,
            'files'=>$files,
            'file'=>$file,
            'nextUrl'=>$log->getNextPageUrl($content,$log_time),
            'prevUrl'=>$log->getPrevPageUrl($content,$log_time)
        ]);
    }
    /**
     * 删除日志
     * @auth true
     * @menu true
     * @return string
     */
    public function remove(){
        $path = $this->request->post('path');
        $res = unlink($path);
        if($res){
            $this->success('删除完成');
        }else{
            $this->error('删除失败','');
        }
    }
}